map_data = [
    "###############################",
    "# p    ## #    #   #  # # #   #",
    "# # # ### # ##### # # ####### #",
    "#   #     ###  #      #   # # #",
    "# # # # # ### # ##### # # # ###",
    "####       # ##  #  #         #",
    "### ### # # # # # ### ### # # #",
    "#                             #",
    "######### # # # ##### # # # # #",
    "#    4 #  #       #           #",
    "# ####### ### # ### # # # # # #",
    "#           #   #  #          #",
    "###############################"
]


def move_4():
    rows = len(map_data)
    cols = len(map_data[0])

    # 找到起始位置和目标位置
    start_pos = None
    p_pos = None
    for i in range(rows):
        for j in range(cols):
            if map_data[i][j] == "4":
                start_pos = (i, j)
            elif map_data[i][j] == "p":
                p_pos = (i, j)

    if start_pos is None or p_pos is None:
        return None
    print(p_pos)
    print(start_pos)

    # 定义移动函数
    def move(position, count):
        x, y = position

        # 边界检查
        if x < 0 or x >= rows or y < 0 or y >= cols or map_data[x][y] == "#":
            return float('inf')

        # 到达目标位置
        if position == p_pos:
            return count

        # 向上、下、左、右移动，并记录最小移动次数
        min_count = float('inf')
        directions = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
        for direction in directions:
            dx, dy = direction
            if 0 <= dx < rows and 0 <= dy < cols and map_data[dx][dy] != "#":
                min_count = min(min_count, move((dx, dy), count + 1))

        return min_count

    # 调用移动函数并返回结果
    min_moves = move(start_pos, 0)
    return min_moves


# 调用移动函数并输出结果
min_moves = move_4()
print(f"The minimum moves to reach 'p' is: {min_moves}")
